package com.xgq.drink.controller;

import com.xgq.drink.entity.AttributeProduct;
import com.xgq.drink.entity.Product;
import com.xgq.drink.entity.ProductResource;
import com.xgq.drink.entity.R;
import com.xgq.drink.service.IAttributeProductService;
import com.xgq.drink.service.IProductResourceService;
import com.xgq.drink.service.IProductService;
import com.xgq.drink.utils.RUtils;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xgq
 * @since 2025-03-25
 */
@RestController
@RequestMapping("/productResource")
public class ProductResourceController {
    @Autowired
    private IProductResourceService productResourceService;
    @Autowired
    private IProductService productService;

    // 根据商品ID获取属性列表
    @GetMapping("/{productId}")
    public R getResourcesByProduct(@PathVariable Long productId) {
        if (!productService.existsProduct(productId)) {
            return RUtils.fail("商品不存在");
        }
        List<ProductResource> resources = productResourceService.getResourcesByProductId(productId);
        return RUtils.success("成功",resources);
    }
    @PostMapping("/create")
    public R createResource(@RequestBody ProductResource resource) {
        if (!productService.existsProduct(resource.getProductId())) {
            return RUtils.fail("商品不存在");
        }
        System.out.println("==================");
        System.out.println(resource);
        productResourceService.save(resource);
        Product product = productService.getById(resource.getProductId());
        product.setImageUrl(resource.getUrl());
        product.setUpdatedAt(LocalDateTime.now());
        productService.updateProduct(product);
        return RUtils.success("商品资源创建成功");
    }
    @DeleteMapping("/delete/{resourceId}")
    public R deleteResource(@PathVariable Long resourceId) {
        productResourceService.removeById(resourceId);
        return RUtils.success("商品资源删除成功");
    }
}
